package org.example.Utils;

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Iterator;

public class ReadExcel {
    public static void main(String[] args) {
        try (InputStream inputStream = new FileInputStream("example.xls")) {
            Workbook workbook = WorkbookFactory.create(inputStream);

            Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表

            Iterator<Row> rowIterator = sheet.rowIterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Cell cell = row.getCell(0); // 获取第一列单元格
                if (cell != null) {
                    switch (cell.getCellType()) {
                        case BLANK:
                            // 空单元格
                            break;
                        case BOOLEAN:
                            boolean booleanValue = cell.getBooleanCellValue();
                            System.out.print(booleanValue + "\t");
                            break;
                        case NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                Date date = cell.getDateCellValue();
                                System.out.print(date + "\t");
                            } else {
                                double doubleValue = cell.getNumericCellValue();
                                System.out.print(doubleValue + "\t");
                            }
                            break;
                        case STRING:
                            String stringValue = cell.getStringCellValue();
                            System.out.print(stringValue + "\t");
                            break;
                        case FORMULA:
                            // 公式类型
                            break;
                        case ERROR:
                            // 错误类型
                            break;
                    }
                }
                System.out.println(); // 输出换行符
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}